package com.guli.mall.pms.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.guli.mall.pms.entity.vo.AttrGroupAndAttrVo;
import com.guli.mall.pms.entity.vo.AttrVo;
import com.guli.mall.pms.entity.vo.AttrWithSelectedAttrListVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.pms.entity.AttrEntity;
import com.guli.mall.pms.service.AttrService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 商品属性
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-17 11:37:23
 */
@RestController
@RequestMapping("pms/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @PostMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取attr分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size,
                  @RequestBody AttrEntity attrEntity) {

        PageUtils<AttrVo> page = attrService.queryPage(currentPage, size, attrEntity);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{attrId}")
    @ApiOperation("根据attrId获取Attr")
    public R info(@PathVariable("attrId") Long attrId) {

        AttrVo attr = attrService.getAttrAndAttrGroupIdById(attrId);

        return R.ok().put("attr", attr);
    }

    @PostMapping("/save")
    @ApiOperation("插入Attr")
    public R save(@RequestBody AttrVo attr) {
        attrService.insertAttrAndRelation(attr);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据attrId更新Attr")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateByIdAndUpdateRelation(attr);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据attrId批量删除Attr")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }


    @PostMapping("/getAttrByCategoryIdAndCondition/{categoryId}")
    @ApiOperation("根据categoryId或者attrType来获取属性，因为销售属性没有绑定属性分组，所以只能直接来这里查询")
    public R getAttrByCategoryIdAndCondition(@PathVariable("categoryId") Long categoryId,
                                             @RequestBody AttrEntity attrEntity){

        List<AttrEntity> attrEntities = attrService.getAttrByCategoryIdAndCondition(categoryId,attrEntity);

        // 给实体类添加一个数组，方便前端操作
        List<AttrWithSelectedAttrListVo> collect = attrEntities.stream().map(attr -> {
            AttrWithSelectedAttrListVo attrWithSelectedAttrListVo = new AttrWithSelectedAttrListVo();

            BeanUtils.copyProperties(attr, attrWithSelectedAttrListVo);

            // 放入数组
            attrWithSelectedAttrListVo.setSelectedAttrList(new ArrayList<>());

            //
            attrWithSelectedAttrListVo.setIsIndeterminate(false);

            attrWithSelectedAttrListVo.setInputVisible(false);

            return attrWithSelectedAttrListVo;
        }).collect(Collectors.toList());

        return R.ok().put("list",collect);
    }





}
